﻿@{ ViewBag.Title = "Index";
    Layout = "~/Areas/Shared/_LayuiList.cshtml"; }

<input style="display: none" name="classid" id="classid" value="@ViewBag.ClassID">

<!--数据表格-->
<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>

<script src="~/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="~/js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script>
    layui.use(["table", "form", "exLayer", "exUtils", "miniPage"], function () {
        let table = layui.table;
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;
        let miniPage = layui.miniPage;

        let $ = layui.$;
        var classid = $("#classid").val();

        let ThisTable = table.render({
            elem: "#tableId",
            url: "/SysTable/Items/GetDetailList?classid=" + $("#classid").val(),
            limit: 10,
            page: true,
            method: "post",
            defaultToolbar: [{ title: '刷新', layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'exports'],
            toolbar: "#toolbarTpl",
            cols: [[
                { field: "DetailCode", title: "子项编码", sort: true, width: 200 },
                { field: "DetailName", title: "子项名称", sort: true },
                { field: "OrderNo", title: "排序", sort: true, width: 100 },
                { title: "操作", width: 180, align: "center", fixed: "right", templet: "#operationTpl" }
            ]],
            done: function (res, curr, count) {
                console.log(res, curr, count);
            }
        });

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "refresh":
                    refresh();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    edit(data.DetailCode);
                    break;
                case "del":
                    del(classid, data.DetailCode);
                    break;
            }
        });

        function refresh() {
            ThisTable.reload({
                where: { classid: $("#classid").val() }
            });
        }

        function add() {
            exLayer.openMiddle("添加", "/SysTable/Items/AddDetail?classid=" + $("#classid").val(), '500px', '450px', null, null);
        }

        function edit(id) {
            exLayer.openMiddle("编辑", "/SysTable/Items/EditDetail?classid=" + $("#classid").val() + "&id=" + id, '500px', '450px', null, null);
        }

        function del(classid, id) {
            exLayer.confirm("确定要删除吗？", function () {
                exUtils.ajax("/SysTable/Items/DeleteDetail", "get", { classid: $("#classid").val(), id: id }, true).done(function (response) {
                    exUtils.tableSuccessMsg(response.message);
                    $(".layui-laypage-btn")[0].click();
                }).fail(function (error) {
                    console.log(error);
                });
            })
        }
    })
</script>
<!-- 头工具栏模板-->
<script type="text/html" id="toolbarTpl">
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add" id="add"><i class="fa fa-plus"></i>添加子项</button>
</script>
<!-- 行工具栏模板 -->
<script type="text/html" id="operationTpl">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" id="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" id="del">删除</a>
</script>